// packageC/video_goods/addVideoGood/addVideoGood.js
var app = getApp();
const storeAlone = 'plugin.store-alone-temp.frontend.videoShare.goods.getGoodsByKeyword';
const getlistStoreAlone = "plugin.store-alone-temp.frontend.videoShare.goods.getRecommendGoods";
Component({
  properties: {},
  data: {
    language: '',
    inputsData: '',
    show1: false,
    goodList: [],
    chooseGoods: null,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    loading: false
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.initData();
      this.getRecommendGoods();
      let language = wx.getStorageSync('langIndex');
      this.setData({ 'language': language.en});
    },
    moved() {},
    detached() {},
  },
  methods:{
    goBack(){
      this.triggerEvent("closeChooseGoods",{goods_id: this.data.chooseGoods})
    },
    initData() {
      this.setData({
        inputsData: '',
        show1: false,
        goodList: [],
        chooseGoods: null
      });
      this.data.inputs = '';
    },
    inpbtn(e) {
      let val = e.detail;
      this.data.inputs = val;
    },
    inpbtnclear() {
      this.setData({
        inputsData: ''
      });
      this.data.inputs = '';
    },
    getData() {
      let that = this;
      that.data.isLoadMore = true;
      that.data.page = 1; 
      that.data.total_page = 0;
      that.data.loading = false;
      let urlStr = app.getNetAddresss(app.globalData.store_alone_temp == 1?storeAlone:"plugin.video-share.frontend.goods.getGoodsByKeyword");
      app._getNetWork({
        url: urlStr,
        data: {
          keyword: this.data.inputs
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              goodList: res.data.list.data,
              total_page:res.data.list.last_page
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    },
    chooseGood(e) {
      let _id = e.currentTarget.dataset.id;
      this.setData({
        chooseGoods: _id
      });
      this.gotoRelease();
    },
    gotoRelease() {
      console.log(app._isTextEmpty(this.data.chooseGoods));
      if (app._isTextEmpty(this.data.chooseGoods)) {
        wx.showToast({
          title: '选择的商品为空',
          icon: 'none',
          duration: 1500
        });
        return;
      } else {
        this.triggerEvent("closeChooseGoods",{goods_id: this.data.chooseGoods})
        // wx.redirectTo({
        //   url: '/packageC/video_goods/releaseVideo/releaseVideo?goods_id=' + this.data.chooseGoods
        // });
      }
    },
    getRecommendGoods() {
      let that = this;
      let urlStr = app.getNetAddresss(app.globalData.store_alone_temp == 1?getlistStoreAlone:"plugin.video-share.frontend.goods.getRecommendGoods");
      app._getNetWork({
        url: urlStr,
        data: {
          page: 1
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              goodList: res.data.list.data,
              total_page:res.data.list.last_page
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      // "plugin.video-share.frontend.goods.getRecommendGoods" : "plugin.video-share.frontend.goods.getGoodsByKeyword"
      console.log(this.data.inputs);
      let url = !this.data.inputs ? (app.globalData.store_alone_temp == 1?getlistStoreAlone:'plugin.video-share.frontend.goods.getRecommendGoods'):(app.globalData.store_alone_temp == 1?storeAlone:'plugin.video-share.frontend.goods.getGoodsByKeyword');
      let urlStr = app.getNetAddresss(url);
      let params = !this.data.inputs ? {
        page: that.data.page + 1
      } : {
        page: that.data.page + 1,
        keyword: this.data.inputs
      };
      that.data.isLoadMore = false; // 防止多次请求分页数据
      if (this.data.page >= this.data.total_page) {
        that.setData({
          loading: true
        });
        return;
      } else {
        this.setData({
          page : this.data.page + 1,
        });
        console.log(this.data.page,params);
        app._getNetWork({
          url: urlStr,
          data: params,
          success: function(resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              var myData = res.data.list.data;
              that.setData({
                goodList: that.data.goodList.concat(myData),
                total_page:res.data.list.last_page
              });
            } else {
              that.data.page = that.data.page - 1;
              that.data.loading = true;
              that.data.isLoadMore = false;
            }
          },
          fail: function(res) {
            console.log(res);
          }
        });
      }
    },
  }
})

